﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.ComponentModel;

namespace WpfCodes.Samples
{
    /// <summary>
    /// Interaction logic for BindingTest.xaml
    /// </summary>
    public partial class BindingTest : Window
    {
        public BindingTest()
        {
            InitializeComponent();
            Binding binding = new Binding("Value") { Source = this.slider1 };
            binding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
            RangeValidationRule rvr = new RangeValidationRule();
            binding.ValidationRules.Add(rvr);
            this.tbxValidate2.SetBinding(TextBox.TextProperty, binding);
        }

        private string _strValue = "StrValue";

        public string StrValue
        {
            get { return _strValue; }
            set { _strValue = value; }
        }

        public class RangeValidationRule : ValidationRule
        {
            public override ValidationResult Validate(object value, System.Globalization.CultureInfo cultureInfo)
            {
                double d = 0;
                if (double.TryParse(value.ToString(), out d))
                {
                    if (d >= 0 && d <= 100)
                    {
                        return new ValidationResult(true, null);
                    }
                }

                return new ValidationResult(false, "Validation Failed.");
            }
        }

    }

    public class Student : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        private int id;
        private int age = 22;
        private string name = "student name";

        public int Id
        {
            get { return id; }
            set { id = value; }
        }
        public int Age
        {
            get { return age; }
            set { age = value; }
        }
        public string Name
        {
            get { return name; }
            set
            {
                name = value;
                //  激发事件
                if (this.PropertyChanged != null)
                {
                    this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs("Name"));
                }
            }
        }
    }

}
